//
//  SongListDetailViewController.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/5/3.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "SongListDetailViewController.h"
#import "SongListDetailTableViewCell.h"
#import "SongListDetailModel.h"
#import "SongListSongsModel.h"
#import "RankListSongListSongsModel.h"
#import "SongListSongsDetailModel.h"
@interface SongListDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) YHYBaseTableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) SongListSongsModel *songsModel;

@end

@implementation SongListDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:0.16 green:0.20 blue:0.25 alpha:1.00];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"歌单详情";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    self.dataSource = [NSMutableArray array];
    
    [self creatView];
    [self dataHandle];


}

#pragma mark - ------------ 数据解析 ---------------
- (void)dataHandle {
    
    NSLog(@"%@", self.songListId);
    
    [YHYAFNetTool GETNetWirhUrl:[NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@", self.songListId] body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        
//        NSLog(@"%@", result);
        
        SongListDetailModel *songListModel = [[SongListDetailModel alloc] initWithDataSource:result];
        self.songsModel = [[SongListSongsModel alloc] initWithDataSource:result];
        NSLog(@"%@", songListModel);
        
        [_dataSource addObject:songListModel];
        
        [self.tableView reloadData];

        
    } Failure:^(NSError *error) {
        
    }];
    
}

#pragma mark - ------------ 创建tableView ---------------
- (void) creatView {
    
    NSLog(@"%@", self.imageStr);
    
    self.tableView = [[YHYBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.tableView registerClass:[SongListDetailTableViewCell class] forCellReuseIdentifier:@"SongListDetailTableViewCellIdentifier"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UIImageView *pic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT *0.3)];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.imageStr]];
    self.tableView.tableHeaderView = pic;
    [self.view addSubview:self.tableView];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (_dataSource.count != 0) {
        SongListDetailModel *songListModel = _dataSource[section];
        return songListModel.songs.count;
    }else {
        return 0;
    }


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
       if (_dataSource.count != 0) {
        SongListDetailModel *songListModel = _dataSource[indexPath.section];
        SongListDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SongListDetailTableViewCellIdentifier"];
        RankListSongListSongsModel *songListSongsModel = songListModel.songs[indexPath.row];
        cell.songListDetailModel = songListSongsModel;
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        return cell;
    }else {
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SongListDetailModel *songListModel = _dataSource[indexPath.section];
//    RankListSongListSongsModel *songListSongsModel = songListModel.songs[indexPath.row];
    SongListSongsDetailModel *model = self.songsModel.songs[indexPath.row];
    if (model.urlList.count == 0 && model.auditionList.count == 0) {
        
        [MBProgressHUD showMessage:@"抱歉 您没有版权"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUD];
            
        });
        
    } else {
        
        [MusicAVPlayer shareMusicAVPlayer].local = indexPath.row;
        [[MusicAVPlayer shareMusicAVPlayer] playWithURL:self.songsModel];
//        [PlayViewController SharePlayViewController].songDetailModel = self.songsModel;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
